package cn.itsource.ymcc.web.controller;

import cn.itsource.ymcc.service.IPayOrderService;
import cn.itsource.ymcc.domain.PayOrder;
import cn.itsource.ymcc.query.PayOrderQuery;
import cn.itsource.ymcc.result.JsonResult;
import cn.itsource.ymcc.result.PageList;
import cn.itsource.ymcc.vo.PayVo;
import com.alipay.easysdk.factory.Factory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/payOrder")
public class PayOrderController {

    @Autowired
    public IPayOrderService payOrderService;

    /**
     * 支付宝 会主动来调用
     *       如果  我们系统宕机了    支付宝最大努力通知
     *       怎么解决这个问题：?
     * @param request
     * @return
     */
    @PostMapping("/notifyUrl")
    public String notifyUrl(HttpServletRequest request){
        try {
            Map<String, String[]> parameterMap = request.getParameterMap();
            Map<String,String> parmeter = new HashMap<>();
            parameterMap.keySet().forEach(key->{
                String[] strings = parameterMap.get(key);
                parmeter.put(key,strings[0]);
            });
            //二次验签  防止数据篡改
            Boolean aBoolean = Factory.Payment.Common().verifyNotify(parmeter);//发送请求到支付宝里面去
            if(aBoolean){ //true 二次验签是成功
                payOrderService.callbackHandle(parmeter);
                return "success";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "failure";
    }

    @PostMapping("/apply")
    public JsonResult apply(@RequestBody PayVo payVo){
        return payOrderService.apply(payVo);
    }

    @GetMapping("/checkPayOrder/{orderNo}")
    public JsonResult checkPayOrder(@PathVariable("orderNo")String orderNo){
        PayOrder order_no = payOrderService.getOne(new QueryWrapper<PayOrder>().eq("order_no", orderNo));
        if(order_no == null){
            return JsonResult.error("未查询到数据");
        }
        return JsonResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody PayOrder payOrder){
        if(payOrder.getId()!=null){
            payOrderService.updateById(payOrder);
        }else{
            payOrderService.save(payOrder);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        payOrderService.removeById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success().setData(payOrderService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.success().setData(payOrderService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody PayOrderQuery query){
        Page<PayOrder> page = new Page<PayOrder>(query.getPageNo(), query.getPageSize());
        page = payOrderService.page(page);
        return JsonResult.success().setData(new PageList<PayOrder>(page.getTotal(), page.getRecords()));
    }
}
